import { defineStore } from 'pinia'
import { getVideoData, updateVideoProgress } from '@/api/video'

export const useVideoStore = defineStore('video', {
  state: () => ({
    currentVideo: null,
    loading: false,
    error: null,
    progress: {},
  }),
  
  actions: {
    async fetchVideoData(lessonId: number) {
      this.loading = true
      try {
        const response = await getVideoData(lessonId)
        this.currentVideo = response.data
        this.error = null
      } catch (err) {
        this.error = err.message
      } finally {
        this.loading = false
      }
    },

    async saveProgress(lessonId: number, progress: number) {
      try {
        await updateVideoProgress(lessonId, progress)
        this.progress[lessonId] = progress
      } catch (err) {
        console.error('Failed to save progress:', err)
      }
    }
  }
})